package no.ntnu.fp.model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

public class lwsGui implements ActionListener {

	JFrame frame;
	int localWatchSystemId = 1;
	JButton MWS;
	
	JButton checkButton, checkButton1, checkButton2, 
	checkButton3, checkButton4, checkButton5, checkButton6;
	
	private Color randomColor(){
		int i = (int) Math.round(Math.random()*3);
		if(i == 0){
			return Color.red;
		}else if(i==2||i==1){
			return Color.green;
		}else{
			return Color.yellow;
		}
	}
	
	private JPanel makeColorPanel(){
		JPanel out = new JPanel(new GridLayout(0,3));
		JPanel jb = new JPanel();
		JPanel jb1 = new JPanel();
		JPanel jb2 = new JPanel();
		JPanel jb3 = new JPanel();
		JPanel jb4 = new JPanel();
		JPanel js = new JPanel();
		jb.setBackground(randomColor());
		js.setBackground(randomColor());
		jb1.setBackground(jb.getBackground());
		jb2.setBackground(jb.getBackground());
		jb3.setBackground(jb.getBackground());
		jb4.setBackground(jb.getBackground());
		
		out.add(jb);
		out.add(jb1);
		out.add(js);
		out.add(jb2);
		out.add(jb3);
		out.add(jb4);
		
		return out;
	}
	
	private JPanel addPanel(String name, JButton b){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(name);
		panel.setBorder(title);
		
		JPanel j = makeColorPanel();
		b = new JButton("Check");
		b.addActionListener(this);
		panel.add(j);
		panel.add(b);
		return panel;
	}
	
	protected void createGui(){
		frame = new JFrame("GUI-eksempel");
		JPanel panel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Local Watch System "+ localWatchSystemId);
		panel.setBorder(title);
		
		JPanel pInfo = new JPanel();
		JPanel subP = new JPanel(new GridLayout(0, 4));
		
		JTextArea jtp = new JTextArea("Green: "+Math.round((Math.random()*20))+" Yellow: "+Math.round((Math.random()*12))+" Red: "+Math.round((Math.random()*4)));
		pInfo.add(jtp);
		//title = BorderFactory.createTitledBorder("Info:");
		//panel.setBorder(title);
		
		subP.add(addPanel("Well1", checkButton1));
		subP.add(addPanel("Well2", checkButton2));
		subP.add(addPanel("Well3", checkButton3));
		subP.add(addPanel("Well4", checkButton4));
		subP.add(addPanel("Well5", checkButton5));
		subP.add(addPanel("Well6", checkButton6));
		
		panel.add(pInfo, BorderLayout.NORTH);
		panel.add(subP);
		MWS = new JButton("Back to Master Watch");
		MWS.addActionListener(this);
		panel.add(MWS, BorderLayout.SOUTH);
		
		frame.setContentPane(panel);
				
		frame.pack();
		frame.setSize(540, 450);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	
	
	public static void main(String[] args) {
		new lwsGui().createGui();

	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("You pushed a button");
		int nr;
		if(e.getSource()==MWS){
			frame.hide();
			new guiTest().createGui();
		}else if(e.getSource()==checkButton){
			nr = 1;
		}else if(e.getSource()==checkButton2){
			nr = 2;
		}else if(e.getSource()==checkButton3){
			nr = 3;
		}else if(e.getSource()==checkButton4){
			nr = 4;
		}else if(e.getSource()==checkButton5){
			nr = 5;
		}else if(e.getSource()==checkButton6){
			nr = 6;
		}
	}

}
